import Vue from "vue"

import VueRouter from "vue-router"

//使用路由懒加载引入组件
const Home = () => import("@pages/Home")
const Search = () => import("@pages/Search");
const Login = () => import("@pages/Login");
const Register = () => import("@pages/Register");
Vue.use(VueRouter)

//1.获取原始的push方法
const lastPush = VueRouter.prototype.push

//2.重写push,覆盖之前的push方法,但是我们的push方法默认传入了onComplate和onAbort参数的默认参数为一个函数,那么如果push出现问题后,就会有catch的函数帮我们处理异常了,这个就是重写push的核心
VueRouter.prototype.push = function (
  location,
  onComplete = () => {},
  onAbort = () => {}
) {
  /* 
    因为之前的push在VueRouter的原型对象上放,所以原本的push中this就是指向VueRouter的实例
    但是现在lastPush被直接默认调用,所以this就指向了undefined了,内部很可能出错
    我们在调用lastPush的时候,可以改变他的this指向VueRouter的实例
  */
  return lastPush.call(this, location, onComplete, onAbort);
};
export default new VueRouter({

  mode: "history",
  routes: [
    //首页
    {
      path: "/home",
      component: Home,
      name: "home"
    },


    //登录
    {
      path: "/login",
      component: Login,
      name: "login",
      meta: { footerIsHidden: true },
    },

    //搜索
    {
      path: "/search",
      component: Search,
      name: "search"
    },

    //注册
    {
      path: "/register",
      component: Register,
      name: "register",
      meta: { footerIsHidden: true },
    },

    //默认进到首页
    {
      path: "/",
      redirect: "/home",
    },
  ]
})